import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/components/Login";
import Register from "@/components/Register";
import JobInfo from "@/components/admin/JobInfo";
import MessageInfo from "@/components/admin/MessageInfo";
import Info from "@/components/user/Info";
import Job from "@/components/user/Job";
import userIndex from "@/components/user/UserIndex";
import adminIndex from "@/components/admin/AdminIndex";
import userInfo from "@/components/admin/UserInfo";
import updateInfo from "@/components/admin/UpdateInfo";
import ClassifyInfo from "@/components/admin/ClassifyInfo";
import likeJob from "@/components/user/LikeJob";
Vue.use(VueRouter)

const routes = [
  {
    path:'/login',
    name:'login',
    component: Login
  },
  {
    path:'/register',
    name:'register',
    component: Register
  },
  {
    path: '/',
    name: 'userIndex',
    component: userIndex,
    redirect:'/job',
    children:[
      {path:'/job',name:'job',component:Job},
      {path:'/info',name:'info',meta: { role: 'user' },component:Info},
      {path:'/likeJob',name:'likeJob',meta: { role: 'user' },component:likeJob},
    ]
  },
  {
    path: '/admin',
    name: 'adminIndex',
    component: adminIndex,
    meta: { role: 'admin'},
    redirect: '/userInfo',
    children:[
      {path:'/userInfo',name:'userInfo',meta: { role: 'admin' },component:userInfo},
      {path:'/updateInfo',name:'updateInfo',meta: { role: 'admin' },component:updateInfo},
      {path:'/classifyInfo',name:'classifyInfo',meta: { role: 'admin' },component:ClassifyInfo},
      {path:'/jobInfo',name:'jobInfo',meta: { role: 'admin' },component:JobInfo},
      {path:'/messageInfo',name:'messageInfo',meta: { role: 'admin' },component:MessageInfo},
    ]
  }

]

const router = new VueRouter({
  routes
})


router.beforeEach((to, from, next) => {
    let role  = to.meta.role;
    let flag=true;
      let Base64 = require('js-base64').Base64;
    if (role==='admin'||role==='user'){
      let token = window.localStorage.getItem('token');
      if (token==null){
        //拦截
        flag=false;
      }else{
        let result = Base64.decode(token);
        if (role==='admin'&&!result.startsWith("admin:")){
          //拦截
          flag=false;
        }
        if (role==='user'&&!result.startsWith("user:")){
          //拦截
          flag=false;
        }
      }
    }
    if (flag) next();
    else next({name:'login'});
})
export default router
